<?php
    namespace Home\Controller;
    use Think\Controller;
    class ShourufentanshezhiController extends Controller {
    
                public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");

        $data = M("cbhs.cbhs_distribution_calc_schm")->limit(($page-1)*$limit,$limit)->select();
        $list = M("cbhs.cbhs_incom_type_dict")->select();


        // for ($i=0; $i < count($data); $i++) 
        // { 
        //     for ($j=0; $j < count($list); $j++) { 
        //         # code...
        //     }
        //     if($data[$i]['cost_type_name'] == $list[$j]['cost_type_name'])
        //     {

        //     }
        // }
        // 
        
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("cbhs.cbhs_distribution_calc_schm")->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $item_class = I("post.item_class");

        $info = M("cbhs.cbhs_distribution_calc_schm")->where("item_class = '$item_class'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $item_class = $data["item_class"];
        unset($data["item_class"]);

        $result = M("cbhs.cbhs_distribution_calc_schm")->where("item_class = '$item_class'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        
        $data["dept_date"] = time();
        $result = M("cbhs.cbhs_distribution_calc_schm")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function get_cbhs_incom_type_dict()
    {
        $list = M("cbhs.cbhs_incom_type_dict")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }


    public function create_data()
    {
    	$data = I('post.');
        $data['date'] = date("Ym",strtotime($data['date']));

        //建立数据库连接
        $user = "system";                //数据库用户名
        $password = "123456";            //密码
        $conn_str = "(DESCRIPTION=(ADDRESS=(PROTOCOL =TCP)(HOST=localhost)(PORT = 1521))(CONNECT_DATA =(SID=orcl)))";          //连接串(cstr : Connection_STRing)
        $remote = true;                  //是否远程连接
        if ($remote) 
        {
          $conn = OCILogon($user, $password, $conn_str);
        }
        else 
        {
          $conn = OCILogon($user, $password);
        }//设定绑定
        $id = 5;                        //准备用以绑定的php变量 id
        $name = "";                     //准备用以绑定的php变量 name/** 调用存储过程的sql语句(sql_sp : SQL_StoreProcedure)
        //  *  语法：
        //  *      BEGIN 存储过程名([[:]参数]); END;
        //  *  加上冒号表示该参数是一个位置
        // **/
        $sql_sp = "BEGIN cbhs.SP_INCOME_AUTO_ACC(:date); END;";
        //Parse
        $stmt = OCIParse($conn, $sql_sp);//执行绑定
        OCIBindByName($stmt, ":date", $data['date'], 16);           //参数说明：绑定php变量$id到位置:id，并设定绑定长度16位Execute
        OCIExecute($stmt);

        $sql_sp = "BEGIN cbhs.SP_INCOME_AUTO_HOS_ACC(:date); END;";
        //Parse
        $stmt = OCIParse($conn, $sql_sp);//执行绑定
        OCIBindByName($stmt, ":date", $data['date'], 16);           //参数说明：绑定php变量$id到位置:id，并设定绑定长度16位Execute
        OCIExecute($stmt);
        
        echo 1;
    }

}